import { updateUserTeacherCert } from '../../../api/user'
import { uploadFileName, wxUpload } from '../../../utils/upload'
import { listByKind } from '../../../api/base'

Page({
  data: {
    teacherList: [],
    showTeacher: false,
    selTeacher: '',
    teacherCertUrls: [],

    teacherCert: {
      authApplyId: null,
      teacherCertCode: '',
      teacherCertType: '',
      teacherCertUrl: ''
    }
  },

  onLoad: function (options) {
    const _this = this
    this.setData({
      teacherCert: getApp().globalData.teacherCert,
      teacherCertUrls: [{url:getApp().globalData.teacherCert.teacherCertUrl}]
    })
    listByKind('teacherCert').then(res => {
      console.log(getApp().globalData.teacherCert.teacherCertType)
      res.forEach(item => {
        if (item.code === getApp().globalData.teacherCert.teacherCertType) {
          _this.setData({
            selTeacher: item.name
          })
        }
      })
      this.setData({
        'teacherList': res
      })
      getApp().globalData.teacherCert = {}
    })
  },

  selectTeacher() {
    this.setData({showTeacher: true})
  },

  cancelSelectTeacher() {
    this.setData({showTeacher: false})
  },

  confirmSelectTeacher(e) {
    this.setData({
      showTeacher: false,
      selTeacher: e.detail.name,
      'teacherCert.teacherCertType': e.detail.code
    })
  },

  editTeacherCertCode(e) {
    this.setData({
      'teacherCert.teacherCertCode': e.detail.value
    })
  },

  afterRead(event) {
    const _this = this;
    const { file } = event.detail;
    const fileName = uploadFileName(file.path);

    wxUpload({
      biz: 'CERTIFICATE',
      fileName: fileName,
      size: file.size,
      filePath: file.path
    }).then(res => {
      _this.setData({
        teacherCertUrls: [{url: res.signatureUrl}],
        'teacherCert.teacherCertUrl': res.uri
      })
    })
  },

  removeImage(e) {
    this.setData({
      teacherCertUrls: [],
      'teacherCert.teacherCertUrl': ''
    })
  },
  done(e) {
    const pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    const reg = new RegExp(/\?[a-zA-Z0-9^%&',;=?$\x22]*/ig, '');
    let teacherCertUrl = '';
    if (reg.test(this.data.teacherCert.teacherCertUrl)) {
      teacherCertUrl = this.data.teacherCert.teacherCertUrl.replace(reg, '')
      this.setData({
        'teacherCert.teacherCertUrl': teacherCertUrl
      })
    }
    const teacherCert = this.data.teacherCert
    if (teacherCert.teacherCertCode === '') {
      wx.showToast({
        title: '请填写资格证编码',
        icon: 'none'
      })
      return false
    } else if (teacherCert.teacherCertType === '') {
      wx.showToast({
        title: '请选择资格证类型',
        icon: 'none'
      })
      return false
    } else if (teacherCert.teacherCertUrl === '') {
      wx.showToast({
        title: '请上传资格证证件照',
        icon: 'none'
      })
      return false
    }
    updateUserTeacherCert(this.data.teacherCert).then(res => {
      prevPage.setData({
        'form.teacherCert': this.data.teacherCert
      })
      wx.navigateBack({
        delta: 1,
      })
    })
  }

})
